﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml;

namespace PKSpeed
{
    /// <summary>
    /// 用户信息
    /// </summary>
    public class User
    {
        public string UserName { get; set; }
        public string PassWord { get; set; }
    }
    public  class UserConfiguration
    {
        //获取app.config路径
        private static readonly string Path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
        /// <summary>
        /// 获取config文件中所有User节点信息并通过User类表示出来
        /// </summary>
        /// <returns>用户信息列表</returns>
      public static List<User> GetUsers()
      {
          var userlist = new List<User>(); //将用户信息用User类保存，返回使用
          var xml = new XmlDocument();
          xml.Load(Path);//读取config文件
          var allUsers = xml.GetElementsByTagName("user");//获取config所有user节点
          if(allUsers.Count==0)
          {
              return null;
          }
            for(int i=0;allUsers[i]!=null;i++)
          {
              var newUser = new User()
              {
                  UserName = allUsers[i].Attributes["userName"].Value, //去属性值
                  PassWord = allUsers[i].Attributes["password"].Value
              };
              userlist.Add(newUser);
          }
          return userlist;
      }
        /// <summary>
        /// 通过用户名获取用户信息
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns>用户信息</returns>
      public static User GetUser(string userName)
      {
          var xml = new XmlDocument();
          xml.Load(Path);//读取config文件
          var allUsers = xml.GetElementsByTagName("user");//获取config所有user节点
          if (allUsers.Count == 0)
          {
              return null;
          }
          for (int i = 0; allUsers[i] != null; i++)
          {
              if (allUsers[i].Attributes["userName"].Value == userName)
             {
                 var user = new User()
                 {
                     UserName = userName, //去属性值
                     PassWord = allUsers[i].Attributes["password"].Value
                 };
                 return user;
             }
          }
          return null;
      }

        /// <summary>
        /// 通过用户名和密码字符串添加用户节点到config
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>添加成功返回true</returns>
      public static bool AddUser(string userName, string password)
      {
          var user=GetUser(userName);
          if (user != null)
          {
              return false;
          }
          else
          {
              var xml = new XmlDocument();
              xml.Load(Path);
              var users = xml.GetElementsByTagName("users")[0];
              var userelement = xml.CreateElement("user");
              userelement.SetAttribute("userName", userName);
              userelement.SetAttribute("password", password);
              users.AppendChild(userelement);
              xml.Save(Path);
              return true;
          }
      }

     
        /// <summary>
        /// 通过用户名和新密码修改密码
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
      public static void ChangePassword(string userName,string password)
      {
          var userlist = new List<User>();
          var xml = new XmlDocument();
          xml.Load(Path);
          var users = xml.GetElementsByTagName("users")[0];
          var allUsers = users.SelectNodes("user");
            for (int i = 0; allUsers[i] != null; i++)
          {
              if (allUsers[i].Attributes["userName"].Value == userName)
              {
                  allUsers[i].Attributes["password"].Value = password;
              }
          }
          xml.Save(Path);
      } 
       
  }
}
